/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { assert, beforeAll, describe, test } from 'vitest';
import { RedundantDependencyCheck } from "../../../src/checker/correctness/RedundantDependencyCheck";
import path from 'path';
import { CHECK_MODE, testCaseCheck } from "./common/testCommon";
import { CheckEntry } from "../../../src/utils/common/CheckEntry";
import { Rule } from '../../../src/Index';

let realPath: string = '';
let checkEntry: CheckEntry;

beforeAll(async () => {
    const rule: Rule = new Rule('');
    checkEntry = await testCaseCheck('./test/unittest/sample/RedundantDependencyCheck/ets', rule, CHECK_MODE.PROJECT2CHECK, RedundantDependencyCheck);
    realPath = checkEntry.scene.getRealProjectDir();
})

describe('RedundantDependencyCheckTest', () => {
    /**
     * @tc.number: RedundantDependencyCheckTest_001
     * @tc.name: 模块级dependencies中无冗余依赖
     * @tc.desc: 模块级dependencies中无冗余依赖
     */
    test('RedundantDependencyCheckTest_001', () => {
        const detectFile: string = path.join(realPath, 'ets', '/entry/oh-package.json5');
        const filteredIssues = checkEntry.projectCheck.issues.filter(issue => {
            const filePath = issue.defect.mergeKey.split('%')[0];
            return filePath === detectFile;
        })
        assert.equal(filteredIssues.length, 0, 'The number of reported line should be 0.');
    })
    /**
     * @tc.number: RedundantDependencyCheckTest_002
     * @tc.name: 模块级dependencies中无冗余依赖
     * @tc.desc: 模块级dependencies中无冗余依赖
     */
    test('RedundantDependencyCheckTest_002', () => {
        const detectFile: string = path.join(realPath, 'ets', '/application/oh-package.json5');
        const filteredIssues = checkEntry.projectCheck.issues.filter(issue => {
            const filePath = issue.defect.mergeKey.split('%')[0];
            return filePath === detectFile;
        })
        assert.equal(filteredIssues.length, 0, 'The number of reported line should be 0.');
    })
    /**
     * @tc.number: RedundantDependencyCheckTest_003
     * @tc.name: 工程级dependencies中有冗余依赖
     * @tc.desc: 工程级dependencies中有冗余依赖
     */
    test('RedundantDependencyCheckTest_003', () => {
        const detectFile: string = path.join(realPath, 'oh-package.json5');
        const filteredIssues = checkEntry.projectCheck.issues.filter(issue => {
            const filePath = issue.defect.mergeKey.split('%')[0];
            return filePath === detectFile;
        })
        assert.isDefined(filteredIssues.length, 'The file path is error.');
        assert.include(filteredIssues[0].defect.description, '@ohos/hamock, @ohos/arkui');
    });
})
